<?php

namespace app\adminmall\controller;
use app\adminmall\model\AddressModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 收货地址
 */
class AddressController extends AdminBaseController
{
 	 protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new AddressModel();
    }
    /**
     * 收货地址列表
     * @author [lx] 
     */
    public function index( $page = 1){
    	$map = [];
    	if(request()->isPost()){
		 	$param = input('post.');
			//用户名称
			if($param['realname']){
				$map['realname'] = ['like', '%'.$param['realname'].'%'];
			}
			//手机号码
			if($param['mobile']){
				$map['mobile'] = ['like', '%'.$param['mobile'].'%'];
			}		 				
	 	}
        $list = $this->model->where($map)->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }


    /**
     * 
     *添加
     * @author [lx] 
     */
    public function add()
    {
        if(request()->isAjax()){
        	$param=input('post.');
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Address/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [lx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Address/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [lx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}